package com.echoesnet.echoesshortvideo.ViewModels;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;

import com.echoesnet.echoesshortvideo.Api.Subscriber.SilenceSubscriber;
import com.echoesnet.echoesshortvideo.AppController;
import com.echoesnet.echoesshortvideo.Beans.CommentListBean;
import com.echoesnet.echoesshortvideo.Constant.Api;
import com.echoesnet.echoesshortvideo.Repositories.CommentListRepository;
import com.echoesnet.echoesshortvideo.Repositories.TinyVideoInfoRepository;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Copyright (C) 2018 科技发展有限公司
 * 完全享有此软件的著作权，违者必究
 * 负责驱动UI
 *
 * @author yqh
 * @version 1.0
 * @modifier
 * @createDate 2018/4/19 18:05
 * @description
 */
public class CommentListViewModel extends ViewModel
{
    private MutableLiveData<CommentListBean> commentListBeanLiveData = new MutableLiveData<>();
    private MutableLiveData<CommentListBean> moreCommentListBeanLiveData = new MutableLiveData<>();
    private MutableLiveData<String> likeComment = new MutableLiveData<>();
    private MutableLiveData<String> deleteComment = new MutableLiveData<>();
    private MutableLiveData<String> commentVideoResult = new MutableLiveData<>();
    private MutableLiveData<String> replyCommentVideoResult = new MutableLiveData<>();
    private CommentListRepository commentListRepository;
    private TinyVideoInfoRepository tinyVideoInfoRepository;
    private int start = 0;
    private int step = 15;

    public LiveData<CommentListBean> getCommentListBeanLiveData()
    {
        return commentListBeanLiveData;
    }

    public LiveData<CommentListBean> getMoreCommentListBeanLiveData()
    {
        return moreCommentListBeanLiveData;
    }

    public LiveData<String> getLikeComment()
    {
        return likeComment;
    }

    public LiveData<String> getDeleteComment()
    {
        return deleteComment;
    }

    public LiveData<String> getCommentVideoResult()
    {
        return commentVideoResult;
    }

    public LiveData<String> getReplyCommentVideoResult()
    {
        return replyCommentVideoResult;
    }

    public CommentListViewModel()
    {
        this.commentListRepository = new CommentListRepository();
        this.tinyVideoInfoRepository = new TinyVideoInfoRepository();
    }

    public void getCommentList(String videoId)
    {
        start = 0;
        commentListRepository.getCommentList(Api.VIDEOC_COMMENT_LIST, videoId, start, step, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                try
                {
                    CommentListBean bean = AppController.getGsonInstance().fromJson(response, new TypeToken<CommentListBean>()
                    {
                    }.getType());
                    commentListBeanLiveData.setValue(bean);
                } catch (JsonSyntaxException e)
                {
                    e.printStackTrace();
                }
            }
        });
    }

    public void getMoreCommentList(String videoId)
    {
        start += step;
        commentListRepository.getCommentList(Api.VIDEOC_COMMENT_LIST, videoId, start, step, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                try
                {
                    CommentListBean bean = AppController.getGsonInstance().fromJson(response, new TypeToken<CommentListBean>()
                    {
                    }.getType());
                    moreCommentListBeanLiveData.setValue(bean);
                } catch (JsonSyntaxException e)
                {
                    e.printStackTrace();
                }
            }
        });
    }

    public void likeComment(String commentId)
    {
        commentListRepository.likeComment(Api.VIDEOC_LIKE_COMMENT, commentId, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                likeComment.setValue("0");
            }
        });
    }

    /**
     * 评论视频
     *
     * @param videoId        视频ID
     * @param commentContent 评论内容
     */
    public void startCommentVideo(String videoId, String commentContent)
    {
        commentListRepository.commentVideo(Api.VIDEOC_COMMENT_VIDEO, videoId, commentContent, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                commentVideoResult.setValue("0");
            }
        });
    }

    /**
     * 回复评论
     *
     * @param videoId        视频ID
     * @param commentContent 评论内容
     */
    public void startReplyComment(String videoId, String commentContent, String commentId, Map<String, String> replyCommentUserInfo)
    {
        commentListRepository.replyComment(Api.VIDEOC_COMMENT_VIDEO, videoId, commentContent, commentId, replyCommentUserInfo, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                replyCommentVideoResult.setValue("0");
            }
        });
    }

    /**
     * 删除评论
     *
     * @param commentId 评论id
     */
    public void deleteComment(String commentId)
    {
        commentListRepository.deleteComment(Api.VIDEOC_DELETE_COMMENT, commentId, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                deleteComment.setValue("0");
            }
        });
    }

    public MutableLiveData<Map<String, String>> checkVideoState(String videoId)
    {
        MutableLiveData<Map<String, String>> checkVideoResult = new MutableLiveData<>();

        tinyVideoInfoRepository.checkVideo(videoId, new SilenceSubscriber<String>()
        {
            @Override
            public void onNext(String response)
            {
                super.onNext(response);
                try
                {
                    JSONObject object = new JSONObject(response);
                    String result = object.getString("delStatus");
                    Map<String, String> map = new HashMap();
                    map.put("videoId", videoId);
                    map.put("result", result);
                    checkVideoResult.setValue(map);
                } catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }
        });

        return checkVideoResult;
    }


}
